<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\AttractionManagement\Model\AttractionSpotPosition;

class AttractionSpotPositionRepository extends IRepository
{
    public function __construct(protected readonly AttractionSpotPosition $model) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'name'), static function (Builder $query, $name) {
                $query->where('name', 'like', '%' . $name . '%');
            })
            ->when(Arr::get($params, 'longitude'), static function (Builder $query, $longitude) {
                $query->where('longitude', $longitude);
            })
            ->when(Arr::get($params, 'latitude'), static function (Builder $query, $latitude) {
                $query->where('latitude', $latitude);
            })
            ->when(Arr::get($params, 'altitude'), static function (Builder $query, $altitude) {
                $query->where('altitude', $altitude);
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->get(['id', 'name', 'longitude', 'latitude', 'altitude']);
    }
}